package com.hualiang.security.filter;


import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import com.hualiang.common.result.Result;
import com.hualiang.common.utils.ResponseUtil;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class TokenLogoutHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Cookie cookie = new Cookie("token", "");
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
        String ajax = request.getHeader("X-Requested-With");
        if (ajax != null && "XMLHttpRequest".equals(ajax)) {
            ResponseUtil.out(response, Result.ok().message("注销成功"));
        } else {
            response.sendRedirect("/");
        }
    }
}
